package control
{
	import com.greensock.*;
	import com.greensock.easing.*;
	
	import control.CardMachine;
	import control.GameRule;
	
	import event.CustomEvent;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.text.TextField;
	
	import view.UIlayer.ScoreboardView;
	
	import vo.CardVo;
	import vo.GameCardsVO;
	import vo.GameMode;
	
	public class GameRule extends EventDispatcher
	{
		private var gameMode:GameMode = GameMode.getInstance();
		
		
		public function GameRule()
		{
			super();
		}	
		
		public function gameStart():void{
			gameMode.initDate();	
			gameMode.gameCardsVO.myCardList.push( new CardVo( gameMode.cardMachine.hitCard() ) );
			dispatchEvent( new CustomEvent(CustomEvent.GAME_DISPLAY) );	
		}
		
		public function gameRreStart():void{
			gameMode.isEnd = false;
			gameMode.initDate();
			dispatchEvent( new CustomEvent(CustomEvent.GAME_START) );	
		}
		
		public function gameGetCard (  ):void{										
			if( getTotalValue( gameMode.gameCardsVO.myCardList ) <= 21 ){
				gameMode.gameCardsVO.myCardList.push( new CardVo( gameMode.cardMachine.hitCard() ) );	
				dispatchEvent( new CustomEvent( CustomEvent.GAME_DISPLAY2 ) );
				//用户摸一张，AI也同时摸一张牌
				//pcGetCard();
			}
			else{		
				gameEnd2();
			}			
		}
		
		public function gameEnd():void{				
				if( gameMode.gameCardsVO.myCardList.length > 0){
					aiGetCardTest();					
				}				
				gameEnd2();		
		}
		
		private function gameEnd2():void{
			gameMode.isWin = isWin();
			gameMode.isEnd = true;
			setChip();
			dispatchEvent( new CustomEvent( CustomEvent.GAME_END ) );	
		}
		
		private function aiGetCardTest():void{		
			var isNoGetCard:Boolean;
			for( ;!isNoGetCard; ){
				if( isGetCard( gameMode.gameCardsVO.pcCardList ) || gameMode.gameCardsVO.pcCardList.length==0 ){
					gameMode.gameCardsVO.pcCardList.push( new CardVo( gameMode.cardMachine.hitCard() )  );	
				}
				else{					
					isNoGetCard = true;
				}
					
			}			
		}	
		
		
		private function pcGetCard():void{		
			if( isGetCard( gameMode.gameCardsVO.pcCardList ) ){
				gameMode.gameCardsVO.pcCardList.push(new CardVo( gameMode.cardMachine.hitCard()));	
			}		
		}
		
		private function isGetCard(cards:Array):Boolean{
			var tempValue:int = getTotalValue( cards );
			
			var isGetCard:Boolean;
			var seed:Number = Math.random();
			if( tempValue < 13 ){
				if(seed <= 0.8)	isGetCard = true;
			}
			else if( tempValue <= 16 ){
				if( seed <= 0.5)	isGetCard = true;
			}
			else if( tempValue <= 19 ){
				if( seed <= 0.2)	isGetCard = true;
			}
			else{
				isGetCard = false;
			}
			//trace("-----------------");
			//trace(tempValue,seed);
			return isGetCard;
		}		
		
		//要有个计算牌堆里总值的方法函数		
		private function getTotalValue ( ary : Array ):int
		{
			var sum:int = 0;
			for(var i:int = 0 ; i < ary.length; i++)
			{
				var cardVo:CardVo = ary[i] as CardVo;
				var cardVal:int = cardVo.value;
				
				if ( cardVal > 10 ){	sum += 10;}
				else {sum += cardVal;}
			}
			return sum
		}		
		
		private function isWin():Boolean{
			var myValue:int = getTotalValue( gameMode.gameCardsVO.myCardList );
			if( myValue > 21 ) return false;
			var pcValue:int = getTotalValue( gameMode.gameCardsVO.pcCardList )
			if( pcValue > 21 ) return true;
			return myValue > pcValue;
				 
		}
		
		//		通过加号控制赢输筹码
		public function add( ):void{			
			if(  gameMode.allChips >=10 ){			
				gameMode.chip += 10;
				gameMode.allChips -= 10;
			}
			gameMode.playMe("assets/sound/sound_1_(split).mp3");
			dispatchEvent( new CustomEvent(CustomEvent.GAME_DISPLAY_SCORE) );
		}
		//		通过减号控制赢输筹码
		public function sub( ):void{			
			if( gameMode.chip > 10 ){ 				
				gameMode.chip -= 10;
				gameMode.allChips += 10;				
			}
			gameMode.playMe("assets/sound/sound_1_(split).mp3");	
			dispatchEvent( new CustomEvent(CustomEvent.GAME_DISPLAY_SCORE) );
		}
		
		private function setChip():void{
			if( gameMode.isWin){
				gameMode.allChips = gameMode.allChips + gameMode.chip*2;
				gameMode.winChip = gameMode.chip*2;
			}
			else{
				gameMode.winChip = -gameMode.chip*2;
				gameMode.allChips = gameMode.allChips - gameMode.chip*2;
			}	
			gameMode.chip = 0;
		}
	}
}